# encoding: utf-8
# @author: wangxin
# @software: garner
# @file: getMemberReport.py
# @time: 2025/2/4 15:24
from calendar import month

import pymysql
from django.http import HttpResponse, JsonResponse
from django.views import View
import datetime

from backend.Maxday import get_max_days_in_month


class getMemberReportView(View):
    def get(self,request):
        now=datetime.datetime.now()
        year=now.year
        month=now.month
        day=get_max_days_in_month(year,month)
        starTime=str(year-1)+"-"+str(month)+"-1"
        endTime=str(year)+"-"+str(month)+"-"+str(day)
        print(starTime,endTime)
        conn = pymysql.Connect(
            host="127.0.0.1",
            port=3306, user="root",
            password="123456",
            db="ruici",
            charset="utf8",
            autocommit=True
        )
        cursor = conn.cursor()
        sql =f"SELECT COUNT(1),t.m FROM (SELECT *,MONTH(regtime) AS m FROM t_member WHERE regTime >='{starTime}' AND regTime <='{endTime}' ) AS t GROUP BY t.m"
        cursor.execute(sql)
        data_result = cursor.fetchall()
        data_dict=dict()
        data_memberCount=list()
        data_months = list()
        for data in data_result:
            data_memberCount.append(data[0])
            data_months.append(data[1])
        data_dict['memberCount']  = data_memberCount
        data_dict['months'] = data_months
        result={
            'code':200,
            'data':data_dict,
        }

        return JsonResponse(result)